<?php

include_once 'lib/facebook.php';
include_once 'includes/config.php';
include_once 'includes/dbconnect.php';
include_once 'tableObjects/TblMember.php';
include_once 'includes/function.php';

if (isset($_POST['btnsubmit']) && (!empty($_SESSION['fb_id']) || $_SESSION['fb_id']!='')) {
    $fb_id = $_SESSION['fb_id'];
    $check_voted = mysql_query("select is_voted from tbl_member where fb_id='$fb_id'");
    $is_voted = mysql_result($check_voted, 0, "is_voted");
    
    
    if ($is_voted == 0) {

        $get_question = mysql_query("select * from tbl_question");

        while ($q = mysql_fetch_assoc($get_question)) {
            
            if ($q['is_multiple']==0) {
                $answer = explode("_", $_POST['checkBox'.$q['id']]);
                
                mysql_query("insert into tbl_answer(question_id,fb_id,answer,created_datetime,submit_from)values('".$q['id']."','$fb_id','".$answer[0]."',now(),'C')") or die(mysql_error());
                
                
            } else {
                $answer=$_POST['checkBox'.$q['id']];
                
                for ($i = 0; $i < count($answer); $i++) {
                    
                    mysql_query("insert into tbl_answer(question_id,fb_id,answer,created_datetime,submit_from)values('".$q['id']."','$fb_id','".$answer[$i]."',now(),'C')") or die(mysql_error());
                }
            }
        }
        
        mysql_query("update tbl_member set is_voted=1,submit_from='C' where fb_id='$fb_id'");
        
        header("Location:landing.php");
    }
    
    
    
    
}
?>